from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import pywinauto


# 初始化浏览器驱动
driver = webdriver.Chrome(ChromeDriverManager().install())
# 浏览器最大化
driver.maximize_window()

driver.get("https://www.baidu.com")

# 隐式等待，一次设置永久有效
driver.implicitly_wait(10)

start_time = time.time()
# 查找搜索设置按钮
try:
    search_setting_ele = driver.find_element(By.XPATH, "//a[@class='setpref first']")
    # 显式等待查找元素，设置的等待时长10s，0.5s检查一次，如果10秒内元素被定位到，代码往下运行，如果超过十秒未定位到元素，报错超时
    # search_setting_ele = WebDriverWait(driver, 10, 0.5).until(EC.presence_of_element_located((By.XPATH, "//a[@class='setpref first']")))
    end_time = time.time()
    print("元素查找耗时：", end_time-start_time)
except:
    end_time = time.time()
    print("元素查找失败，耗时：", end_time-start_time)

search_setting_ele.click()

# 打开网易见外工作台首页
# driver.get("https://jianwai.youdao.com/")
# driver.implicitly_wait(10)
#
# # 切换表单，切换进入内联框架
# iframe_ele = driver.find_element(By.CSS_SELECTOR, "iframe[id]")  # 定位表单
# driver.switch_to.frame(iframe_ele)  # 切换进入表单
#
# # 输入邮箱
# driver.find_element(By.CSS_SELECTOR, "input[name=email]").send_keys("974381044@qq.com")
#
# # 输入密码
# driver.find_element(By.XPATH, "//input[@name='password']").send_keys("zytc@12310120")
#
# # 点击-登录
# driver.find_element(By.ID, "dologin").click()
#
# # 点击-新建项目
# driver.find_element(By.XPATH, "//div[@class='btn btn-add']").click()
# # 点击-图片翻译
# driver.find_element(By.XPATH, "//div[@class='title' and text()='图片翻译']").click()
#
# # input标签文件上传
# driver.find_element(By.CLASS_NAME, "file").send_keys(r"C:\TT\习知\测开代码\python-basic-warehouse-td2\selenium-下\1624958787.png")
#

time.sleep(5)
driver.quit()